import uvicorn
from fastapi import FastAPI
from typing import Optional
from pydantic import BaseModel
from enum import Enum

# 预设值
class Modelname(str,Enum):
    alexnet = "alexnet"
    resnet = "resnet"
    lenet = "lenet"

app = FastAPI(
    title='大标题',
    description='整体描述信息',
    version='0.1.0',
    contact={
        'name':'john Doe',
        'email':'john@example.com',
    },
    license_info={
        'name':'MIT',
    },
)

@app.get("/models/{model_name}")
async def get_model(model_name:Modelname):
    if model_name is Modelname.alexnet:
        return {"model_name": model_name,"message":"Deep learning FTW!"}
    if model_name.value == "lenet":
        return {"model_name": model_name,"message":"leCNN all the images"}
    return {"model_name": model_name,"message":"Have some residuals"}




@app.get("/")
async def root():
    return {"message": "Hello World"}

# 路径参数
@app.get("/items/me")
async def read_user_me():
    return {"user_id": "the current user"}

@app.get("/items/{item_id}")
async def read_items(item_id: int):
    return {"item_id": item_id}

# 查询参数
@app.get("/items")
async def read_items(skip: int = 0, limit: int = 100):
    return {"skip": skip, "limit": skip + limit}

# 请求体
class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None

@app.post("/items")
async def create_item(item: Item):
    return item

@app.get("/items",tags=["items"])
async def read_items():
    return [{'name':'item 1'}]

@app.get("/users",tags=["users"])
async def read_users():
    return [{'username':'johndoe'}]

@app.post("/items/",
          summary='接口注释说明',
          description='接口描述信息'
          )
async def create_item(item: Item):
    return item

@app.get("/items/")
def items_parameters(skip: int = 0, limit: int = 10):

    '''
    :param skip: 起始行数
    :param limit: 每页条目数
    :return: 来来俩
    '''
    return {"messgae": skip, "limit": skip + limit}





if __name__ == '__main__':
    uvicorn.run('lianxi:app', host='localhost', port=8000,reload=True)